package org.wejde.muel;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.packet.DelayInformation;

/**
 * This class contains the JComponents and functions
 * related to chatroom.
 * 
 * The JComponent in the class is the textArea.
 * 
 * The methods include adding messages to the window and
 * processing inbound XMPP packets.
 * 
 * Because the chatroomPane is expected to display XMPP
 * messages, the class implements the PacketListener
 * interface.
 * 
 * @author Eric Martin
 * @author Webb Pinner
 *
 */
public class chatroomPane extends JPanel implements PacketListener {

	/**
	 * How the time is formatted when events are displayed
	 */
	private static final String TIMEFORMAT = "HH:mm:ss";
	
	/**
	 * The TextArea for displaying events, this component
	 * lives in a JScrollPane. 
	 */
	private JTextArea chatroom;
	
	/**
	 * The JScrollPane that contains the JTextArea
	 */
	private JScrollPane chatroomScroll;
	
	/**
	 * Serial Version UID
	 */
	private static final long serialVersionUID = 1296697118560292532L;

	/**
	 * Constructor method
	 */
	public chatroomPane() {
		super();
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.setMinimumSize(new Dimension(570, 224));
		this.setSize(this.getMinimumSize());
		this.chatroom = new JTextArea(13,49);
		this.chatroom.setEditable(false);
		this.chatroom.setLineWrap(true);
		this.chatroomScroll = new JScrollPane(chatroom, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		this.add(chatroomScroll);
	}

	/**
	 * Method to add text (events) to the chatroom window.
	 * After text is added, the window scrolls to the
	 * bottom to display only the most recent events.
	 * 
	 * @param msg the message to add to the chatroom.
	 */
	public void addEventMsg(String msg) {
		this.chatroom.append(msg);
		this.chatroom.scrollRectToVisible(getBounds());
	}
	
	/**
	 * Method to process the incoming XMPP packets.  This
	 * is a required method as part of the PacketListener
	 * interface.
	 * 
	 * @param packet the incoming XMPP packet.
	 */
	public void processPacket(Packet packet) {
		
		Message msg = (Message) packet;
		
		// Build the message to display
		// Parse out and add just the JID from the packet
		// sender
		String from = msg.getFrom().split("/")[1];
		
		// Add the time to the message arrived.
		SimpleDateFormat timefmt = new SimpleDateFormat(chatroomPane.TIMEFORMAT);
		Date timestamp;
		String when;
		
		
		DelayInformation inf = null;
		try {
			inf = (DelayInformation)packet.getExtension("x","jabber:x:delay");
			timestamp = inf.getStamp();
		} catch (Exception e) {
			timestamp = new Date();
		}

		when = "("	+ timefmt.format(timestamp) + ")";

		// Add the contents of the event.
		String event = msg.getBody();

		// Send the event to the chatroom window
		this.addEventMsg(when + " " + from + ": " + event);
	}
}
